package gugga.collections.dictionary
{
	import flash.utils.Dictionary;
	
	/**
	 * <code>Dictionary</code> class is designed to substitute the <code>HashTable</code> 
	 * class from the Actionscript 2 GuggaFF, in order to take advantage of the native 
	 * for the Actionscript 3 <code>Dictionary</code> class. GuggaFF <code>Dictionary</code> 
	 * class is extending the Actionscript 3 <code>Dictionary</code> class to adapt its 
	 * interface to the interface of the previously used <code>HashTable</code>.
	 */
	public dynamic class Dictionary extends flash.utils.Dictionary
	{
		public function Dictionary(aWeakKeys : Boolean = false, aOriginalDictionary : flash.utils.Dictionary = null)
		{
			super(aWeakKeys);
			
			if(aOriginalDictionary)
			{
				for(var key : * in aOriginalDictionary)
				{
					this[key] = aOriginalDictionary[key];
				}
			}
		}
		
		public function add(aKey : *, aValue : *) : void
		{
			this[aKey] = aValue;
		}
		
		public function remove(aKey : *) : Object
		{
			var result : Object = this[aKey]; 
			delete this[aKey];
			return result;
		}
		
		public function containsValue(aValue : *) : Boolean
		{
			var result : Boolean = false;
			for(var key : * in this) 
			{
				if(this[key] == aValue)
				{
					result = true;
					break;
				}
			}
			return result;
		}
		
		public function containsKey(aKey : *) : Boolean
		{
			var result : Boolean = false;
			if(this[aKey] != undefined && this[aKey] != null)
			{
				result = true;
			}
			return result;
		}
		
		public function getKeyByValue(aValue : *) : *
		{
			var result : * = null;
			for(var key : * in this) 
			{
				if(this[key] == aValue)
				{
					result = key;
					break;
				}
			}
			return result;
		}
		
		public function getValue (aKey : *) : Object 
		{
			return this[aKey];	
		} 
		
		public function get IsEmpty() : Boolean
		{
			for(var key : String in this) 
			{
				return false;
			}
			return true;
		}
		
		public function get count() : Number
		{
			var itemsCount : Number = 0;
			for(var key : String in this)
			{
				itemsCount++;
			}
			return itemsCount;
		}
		
		public function clone() : gugga.collections.dictionary.Dictionary
		{
			var result : gugga.collections.dictionary.Dictionary = 
				new gugga.collections.dictionary.Dictionary();
			for (var key : * in this)
			{
				result.add(key, this[key]);
			}
			return result;
		}
		
		/**
		 * Returns an array containing all elements of the <code>Dictionary</code>.
		 */
		private function getAllElements() : Array
		{
			var result : Array = new Array();
			for (var i : String in this) 
			{
				result.push(this[i]);
			}
			return result;
		}
	}
}